package com.baidu.shopping.pojo;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

public class Cart {
	
	private String id;
	
	private Map<String,CartItem> cart = new HashMap<String, CartItem>();

	
	
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}
    
	public Map<String, CartItem> getCart() {
		return cart;
	}

	public void setCart(Map<String, CartItem> cart) {
		this.cart = cart;
	}
	
	
	
	
    //添加购物车
	public void add(CartItem item) {
		
		if(cart.containsKey(item.getApp().getId())){
			CartItem innerItem = cart.get(item.getApp().getId());
			innerItem.setBuyNum(innerItem.getBuyNum() + item.getBuyNum());
		} else {
			cart.put(item.getApp().getId(), item);
		}
	}
	
	
	
	public void remove(String appid) {
		cart.remove(appid);
	}
	
	
	public void update(String appid, int buyNum) {
		CartItem cartItem = cart.get(appid);
		if(cartItem != null) {
			cartItem.setBuyNum(buyNum);
		}
	}
	
	
	public void clear() {
		cart.clear();
	}
	
	
	public double getTotalPrice() {
		
		double totalPrice = 0.0;
		Collection<CartItem> items = cart.values();
		for (CartItem cartItem : items) {
			totalPrice += cartItem.getTotalPrice();
		}
		return totalPrice;
		
	}
	
	
	
	
}
